package com.ald.plane_tool.ui.fragment;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.view.Gravity;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.ald.plane_tool.BR;
import com.ald.plane_tool.R;
import com.ald.plane_tool.databinding.FragmentRockerBinding;
import com.ald.plane_tool.ui.fragment.viewpager.AmerikaanseHandFragment;
import com.ald.plane_tool.ui.fragment.viewpager.ChinaHandFragment;
import com.ald.plane_tool.ui.fragment.viewpager.JapanHandFragment;
import com.ald.plane_tool.utlis.DialogUtlis;
import com.ald.plane_tool.viewmodle.RockerViewModle;
import com.ald.plane_tool.viewpageradapater.MyPagerAdapter;
import com.aleyn.mvvm.base.BaseFragment;
import com.google.android.material.tabs.TabLayout;

/*
 * 摇杆
 * */
public class RockerFragment extends BaseFragment<RockerViewModle, FragmentRockerBinding> {

    private Fragment[] Lfragments = {new JapanHandFragment(), new AmerikaanseHandFragment(), new ChinaHandFragment()};
    //未选中图片
    private int[] Limg = {R.mipmap.ribenshoua,R.mipmap.tab_map,R.mipmap.zhongguoshouc};
    private String[] Ltitles = {"日本手", "美国手", "中国手"};
    //选中图片
    private int[]  Limgn = {R.mipmap.ribenshouaa,R.mipmap.meiguoshoua,R.mipmap.zhongguoshoua};
    //配置默认选中第几项

    private int ItemWhat=1;
    @Override
    public int initVariableID() {
        return BR.RockerViewModle;
    }

    @SuppressLint("WrongConstant")
    @Override
    public void lazyLoadData() {
        MyPagerAdapter myFragmentPagerAdapter = new MyPagerAdapter(getChildFragmentManager(),getActivity(),Lfragments,Ltitles);
        mBinding.viewPager.setAdapter(myFragmentPagerAdapter);
        //设置未选中和选中时字体的颜色
        mBinding.tab.setSelectedTabIndicatorHeight(0);
        mBinding.tab.setTabTextColors(getResources().getColor(R.color.base_cssolor), getResources().getColor(R.color.base_cssolor));
        mBinding.tab.setTabMode(TabLayout.MODE_FIXED);
        mBinding.tab.setupWithViewPager( mBinding.viewPager);

        for (int i = 0; i < 3; i++) {
            TabLayout.Tab tab = mBinding.tab.getTabAt(i);
            //注意！！！这里就是添加我们自定义的布局
            tab.setCustomView(myFragmentPagerAdapter.getCustomView(i));
            //这里是初始化时，默认item0被选中，setSelected（true）是为了给图片和文字设置选中效果，代码在文章最后贴出
            if (i == 0) {
                (tab.getCustomView().findViewById(R.id.iv)).setSelected(true);

            }
        }
        Dialog dialogzwl = DialogUtlis.dialogzwl(getActivity(), R.layout.dialog_caouzuomoshiqueding, Gravity.CENTER, 1, 1);
        TextView tv01 = dialogzwl.findViewById(R.id.tv_01);
        mBinding.tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()){
                    case 0:
                        tv01.setText("确认选择日本手操作模式");
                        break;
                    case 1:
                        tv01.setText("确认选择美国手操作模式");
                        break;
                    case 2:
                        tv01.setText("确认选择中国手操作模式");
                        break;
                }
                dialogzwl.show();
                (tab.getCustomView().findViewById(R.id.iv)).setSelected(true);
                mBinding.viewPager.setCurrentItem(tab.getPosition());
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                (tab.getCustomView().findViewById(R.id.iv)).setSelected(false);

            }
            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });




    }



    @Override
    public int layoutId() {
        return R.layout.fragment_rocker;
    }

}
